﻿'20130531
Imports System.IO
Namespace Utilities.FTP
    Class FTPclient_plus
        Inherits FTPclient
        Sub New()
            MyBase.New()
        End Sub
        Sub New(ByVal Hostname As String)
            MyBase.new(Hostname)

        End Sub
        Sub New(ByVal Hostname As String, ByVal Username As String, ByVal Password As String)
            MyBase.New(Hostname, Username, Password)

        End Sub


        Public Function UploadDirectory(ByVal localDirectoryname As String, Optional ByVal targetDirectoryname As String = "") As Boolean
            Dim isSuccess As Boolean = True
            Try
                Dim tDirectoryFileName As String() = Directory.GetFiles(localDirectoryname)
                '  isSuccess = MyBase.Upload(localDirectoryname, targetDirectoryname)
                MyBase.FtpCreateDirectory(targetDirectoryname)
                '檔案
                Dim tFileName As String
                Dim tPath As String()
                For index As Integer = 0 To tDirectoryFileName.Length - 1
                    tPath = tDirectoryFileName(index).Split("\")
                    tFileName = tPath(tPath.Length - 1)
                    isSuccess = isSuccess And MyBase.Upload(tDirectoryFileName(index), targetDirectoryname + tFileName)
                Next
                '資料夾

                Dim tDirectoryPath As String
                Dim tDirectoryName As String() = Directory.GetDirectories(localDirectoryname)
                For index As Integer = 0 To tDirectoryName.Length - 1
                    tPath = tDirectoryName(index).Split("\")
                    tDirectoryPath = tPath(tPath.Length - 1)
                    isSuccess = isSuccess And Me.UploadDirectory(tDirectoryName(index) + "\", targetDirectoryname + tDirectoryPath + "\")
                Next
            Catch ex As Exception
                Return False
            End Try

            ''檔案
            'For index As Integer = 0 To tDirectoryName.Count - 1

            '    Dim newPath As String = localDirectoryname + "\" + tDirectory(index).Filename
            '    If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.File Then
            '        isSuccess = isSuccess And MyBase.Download(tDirectory(index).FullName, newPath, True)
            '    End If
            '    '資料夾
            '    If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.Directory Then

            '        isSuccess = isSuccess And Me.DownloadDirectory(tDirectory(index).FullName, newPath)
            '    End If
            'Next
            Return isSuccess

        End Function

        Public Function DownloadDirectory(ByVal sourceDirectoryname As String, ByVal localDirectoryname As String) As Boolean
            Directory.CreateDirectory(localDirectoryname)
            Dim tDirectory As FTPdirectory = MyBase.ListDirectoryDetail(sourceDirectoryname + "/")
            Dim isSuccess As Boolean = True
            For index As Integer = 0 To tDirectory.Count - 1
                '檔案
                Dim newPath As String = localDirectoryname + "\" + tDirectory(index).Filename
                If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.File Then
                    isSuccess = isSuccess And MyBase.Download(tDirectory(index).FullName, newPath, True)
                End If
                '資料夾
                If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.Directory Then

                    isSuccess = isSuccess And Me.DownloadDirectory(tDirectory(index).FullName, newPath)
                End If
            Next
            Return isSuccess
        End Function
        Public Function deletedDirectory(ByVal sourceDirectoryname As String) As Boolean

            Dim tDirectory As FTPdirectory = MyBase.ListDirectoryDetail(sourceDirectoryname)
            Dim isSuccess As Boolean = True
            For index As Integer = 0 To tDirectory.Count - 1
                '檔案

                If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.File Then
                    isSuccess = isSuccess And MyBase.FtpDelete(tDirectory(index).FullName)
                End If
                '資料夾
                If tDirectory(index).FileType = FTPfileInfo.DirectoryEntryTypes.Directory Then

                    isSuccess = isSuccess And Me.deletedDirectory(tDirectory(index).FullName + "/")
                    isSuccess = isSuccess And Me.FtpDeleteDirectory(tDirectory(index).FullName + "/")
                End If
            Next
            Return isSuccess
        End Function
        Function IsFTPAlive() As Boolean
            'create request
            Try
                Me.ListDirectoryDetail()
                Return True
            Catch ex As Exception
                Return False
            End Try

        End Function
    End Class
End Namespace
